package cn.mymall.web.controller.houtai.mall;

import cn.mymall.entity.MemberAddressOrder;
import cn.mymall.entity.OrderItem;
import cn.mymall.entity.TradeOrder;
import cn.mymall.help.PageVo;
import cn.mymall.service.IMemberAddressOrderService;
import cn.mymall.service.IOrderItemService;
import cn.mymall.service.ITradeOrderService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.BeanUtil;
import cn.mymall.vo.order.OrderSearchResultVo;
import cn.mymall.vo.order.OrderSearchVo;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderApiController extends BaseHoutaiController {
    @Autowired
    private IOrderItemService orderItemService;
    @Autowired
    private ITradeOrderService orderService;
    @Autowired
    private IMemberAddressOrderService memberAddressOrderService;

    @RequestMapping("/api/order/list")
    public ResultCode order(OrderSearchVo reqVo) {
        Page<TradeOrder> page = orderService.getPageList(reqVo);
        PageVo pageVo = PageVo.getInstance(page);
        List<TradeOrder> orderList = page.getRecords();
        List<OrderSearchResultVo> orderSearchResultVoList = new ArrayList<>();
        BeanUtil.copyListProperties(orderList, orderSearchResultVoList, OrderSearchResultVo.class);
        for (OrderSearchResultVo orderSearchResultVo : orderSearchResultVoList) {
            TradeOrder order = orderService.getById(orderSearchResultVo.getId());
            List<OrderItem> orderItemList = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", orderSearchResultVo.getId()));
            orderSearchResultVo.setOrderItemList(orderItemList);
            orderSearchResultVo.setQuantity(orderItemList.size());
            orderSearchResultVo.setOrderStatusText(order.gainOrderStatusText());
            orderSearchResultVo.setOrderAmount(order.getOrderAmount());
            orderSearchResultVo.setPayStatusText(order.gainPayStatusText());
            orderSearchResultVo.setProductName(orderItemList.get(0).getProductName());
        }
        pageVo.setRecords(orderSearchResultVoList);
        return ResultCode.getSuccessReturn(pageVo);
    }

    @RequestMapping("/api/order/detail")
    public ResultCode<Map> order(Integer orderId, ModelMap model) {
        TradeOrder order = orderService.getById(orderId);
        MemberAddressOrder memberAddressOrder = memberAddressOrderService.getOne(new QueryWrapper<MemberAddressOrder>().eq("order_id", orderId));
        List<OrderItem> orderItemList = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getId()));
        model.put("order", order);
        model.put("memberAddressOrder", memberAddressOrder);
        model.put("orderItemList", orderItemList);
        Map resMap = new HashMap<>();
        resMap.put("order", order);
        resMap.put("address", memberAddressOrder);
        resMap.put("orderItemList", orderItemList);
        return ResultCode.getSuccessReturn(resMap);
    }
}
